<template>
  <view class="content">
    <view class="form">
      <view class="item">
        <view class="label">
          <image class="starImg" src="../../static/img/star.png" mode=""></image>
          学生姓名
        </view>
        <view class="ipt" @tap="studentShow = true">
          <view class="text">
            {{studentText}}
          </view>
          <image class="lowerImg" src="../../static/img/lowerNew.png" mode=""></image>
        </view>
      </view>
      <view class="item">
        <view class="label">
          <image class="starImg" src="../../static/img/star.png" mode=""></image>
          测试日期
        </view>
        <view class="ipt" @tap="timeShow = true">
          <view class="time">
            <image class="timeImg" src="../../static/img/time.png" mode=""></image>
            {{info.test_date}}
          </view>
        </view>
      </view>
      <view class="item">
        <view class="label">
          <image class="starImg" src="../../static/img/star.png" mode=""></image>
          测试类型
        </view>
        <view class="ipt" @tap="hourShow = true">
          <view class="text">
            {{hourText}}
          </view>
          <image class="lowerImg" src="../../static/img/lowerNew.png" mode=""></image>
        </view>
      </view>
      <view class="item">
        <view class="label">
          <image class="starImg" src="../../static/img/star.png" mode=""></image>
          测试分数
        </view>
        <view class="ipt">
          <input type="number" placeholder="请输入分数" v-model="info.score">
        </view>
      </view>
      <view class="textBox">
        <view class="label">
          <image class="starImg" src="../../static/img/star.png" mode=""></image>
          测试说明
        </view>
        <view class="ipt">
          <u--textarea placeholder="请输入测试说明" placeholderStyle="font-size: 28rpx;color: red;"
            v-model="info.content"></u--textarea>
        </view>
      </view>
    </view>
    <view class="btn" @tap="editEducational()">
      确认提交
    </view>
    <!-- 日期选择框 -->
    <u-calendar :show="timeShow" mode="single" @confirm="confirmTime" @close="timeShow = false"
      :closeOnClickOverlay="true" :minDate="minDate" monthNum="13"></u-calendar>
    <!-- 测试类型下拉框 -->
    <u-picker :show="hourShow" :columns="hourColumns" keyName="label" @confirm="confirmHour"
      @cancel="hourShow = false"></u-picker>
    <!-- 学生下拉框 -->
    <u-picker :show="studentShow" :columns="studentColumns" keyName="username" @confirm="confirmStudent"
      @cancel="studentShow = false"></u-picker>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        time: '',
        timeShow: false,
        hourShow: false,
        studentShow: false,
        studentText: "请选择学生",
        hourText: "测试类型",
        minDate: '',
        hourColumns: [
          [{
            label: '入门测',
            // 其他属性值
            id: 1
            // ...
          }, {
            label: '模块测',
            id: 2
          }, {
            label: '阶段测',
            id: 3
          }, {
            label: '出门测',
            id: 4
          }]
        ],
        studentColumns: [],
        value1: '',
        info: {
          id: '',
          user_id: '',
          test_date: '',
          type: '',
          score: '',
          content: ''
        }
      };
    },
    onLoad(option) {
      this.chooseTimed()
      this.getEducationalDetail(option.id)
      this.getTeacherStudents()
    },
    methods: {
      // 设置默认日期，最大值为今天，最小值为去年今天
      chooseTimed() {
        let date = new Date();
        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        let day = date.getDate();
        if (month <= 9) {
          month = '0' + month;
        }
        if (day <= 9) {
          day = '0' + day;
        }
        let minyear = year - 1;
        this.minDate = minyear + '-' + month + '-' + day;
      },

      confirmTime(e) {
        this.info.test_date = e[0];
        this.timeShow = false
      },
      confirmHour(e) {
        this.hourText = e.value[0].label
        this.info.type = e.value[0].id
        this.hourShow = false
      },
      confirmStudent(e) {
        this.studentText = e.value[0].username
        this.info.user_id = e.value[0].id
        this.studentShow = false
      },
      async getTeacherStudents() {
        const {
          data: res
        } = await uni.$http.post('teacher/others/getTeacherStudents')
        if (res.code != 1) {
          return this.toast.msg(res.msg)
        }
        res.data.forEach(i => {
          if (i.id == this.info.user_id) {
            this.studentText = i.username
          }
        })
        this.studentColumns.push(res.data)
      },
      async editEducational() {
        const {
          data: res
        } = await uni.$http.post('teacher/others/editEducational', this.info)
        if (res.code != 1) {
          return this.toast.msg(res.msg)
        }
        setTimeout(() => {
          this.toast.msg('修改成功')
          uni.navigateTo({
            url: '../measure/measure'
          })
        }, 500)
      },
      async getEducationalDetail(id) {
        const {
          data: res
        } = await uni.$http.post('teacher/others/getEducationalDetail', {
          id: id
        })
        if (res.code != 1) {
          return this.toast.msg(res.msg)
        }
        if (res.data.type == 1) {
          this.hourText = '入门测'
        }
        if (res.data.type == 2) {
          this.hourText = '模块测'
        }
        if (res.data.type == 3) {
          this.hourText = '阶段测'
        }
        if (res.data.type == 4) {
          this.hourText = '出门测'
        }
        this.info.id = res.data.id
        this.info.content = res.data.content
        this.info.test_date = res.data.test_date
        this.info.score = res.data.score
        this.info.type = res.data.type
        this.info.user_id = res.data.user_id
      }
    }
  }
</script>

<style lang="scss">
  .content {
    min-height: 100vh;

    .form {
      width: 652rpx;
      margin: 40rpx auto;

      .item {
        display: flex;
        align-items: center;
        padding: 0 35rpx 0 19rpx;
        margin-bottom: 30rpx;
        width: 652rpx;
        height: 78rpx;
        line-height: 78rpx;
        border-radius: 10rpx;
        border: 2rpx solid #FCF7F7;

        .label {
          display: flex;
          align-items: center;
          font-size: 28rpx;
          margin-right: 70rpx;

          .starImg {
            width: 14rpx;
            height: 42rpx;
            margin-right: 15rpx;
          }
        }

        .ipt {
          flex: 1;
          position: relative;
          display: flex;
          align-items: center;
          color: #666;

          .lowerImg {
            position: absolute;
            right: 0;
            width: 20rpx;
            height: 11rpx;
          }
        }

        .time {
          color: #666;
          display: flex;
          align-items: center;

          .timeImg {
            width: 35rpx;
            height: 35rpx;
            margin-right: 17rpx;

          }
        }
      }

      .textBox {
        padding: 0 35rpx 0 19rpx;
        display: flex;
        border: 2rpx solid #FCF7F7;

        .label {
          height: 70rpx;
          display: flex;
          align-items: center;
          margin-right: 70rpx;

          .starImg {
            width: 14rpx;
            height: 42rpx;
            margin-right: 15rpx;
          }
        }

        .ipt {
          flex: 1;
          height: 352rpx;

          .u-textarea {
            height: 352rpx;
            border: none;
            padding-left: 0;

            /deep/.u-textarea__field {
              height: 352rpx !important;
            }

            .uni-textarea-textarea {
              height: 352rpx;
            }
          }
        }
      }
    }

    .btn {
      position: fixed;
      bottom: 50rpx;
      left: 50%;
      transform: translateX(-50%);
      width: 652rpx;
      height: 78rpx;
      line-height: 78rpx;
      text-align: center;
      background: #CCCCCC;
      border: 2rpx solid #69A1E0;
    }
  }
</style>